package com.heima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heima.reggie.bean.*;
import com.heima.reggie.common.R;
import com.heima.reggie.dto.SetmealDto;
import com.heima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "套餐管理")
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R selectByPage(PageParam pageParam) {
        System.out.println(pageParam);
        IPage<SetmealDto> setmealDtoIPage = setmealService.selectByPage(pageParam);
        if (setmealDtoIPage != null) {
            return R.success(setmealDtoIPage);
        } else {
            return R.error("查询失败！");
        }
    }

    @PostMapping
    @ApiOperation(value = "新增套餐")
    public R add(@RequestBody SetmealDto setmealDto) {
        System.out.println(setmealDto);
        int add = setmealService.add(setmealDto);
        if (add > 0) {
            return R.success("添加成功！");
        } else {
            return R.error("添加失败！");
        }

    }

    @GetMapping
    @ApiOperation(value = "套餐删除")
    //@ApiImplicitParams({@ApiImplicitParam(name = "ids",value = "批量id",required = true)})
    public R deletetByIds(@RequestParam List<Long> ids) {
        System.out.println(ids);
        int i = setmealService.deletetByIds(ids);
        if (i > 0) {
            return R.success("删除成功！");
        } else {
            return R.error("删除失败！");
        }
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "更新套餐状态")
    public R updateStatus(@PathVariable int status, @RequestParam List<Long> ids) {
        System.out.println(status);
        System.out.println(ids);
        DishSi dishSi = new DishSi(status, new ArrayList<>(ids));
        int i = setmealService.updateStatus(dishSi);
        if (i > 0) {
            return R.success("更新状态成功！");
        } else {
            return R.error("更新状态失败！");
        }
    }

    @PutMapping
    @ApiOperation(value = "更新套餐")
    public R update(@RequestBody SetmealDto setmealDto) {
        int i = setmealService.update(setmealDto);
        if (i > 0) {
            return R.success("更新套餐成功！");
        } else {
            return R.error("更新套餐失败！");
        }
    }

    @GetMapping("/list")
    @ApiOperation(value = "套餐条件查询")
    public R findByCid(Setmeal setmeal) {
        List<Setmeal> list = setmealService.findByCid(setmeal);
        if (list!=null) {
            return R.success(list);
        } else {
            return R.error("查询套餐失败！");
        }
    }
}
